if (!base.profiles) base.profiles = {};

base.profiles = {
	init:function(){
		if($('.topicsWrapper')[0]){
			this.topicHandler();
			this.topics = $('.topicsWrapper .topic');
		}
	},
	topicHandler: function(){
		me = this;
		$(".topic .on").off().on("click", function(e){
			if(!$(e.target).hasClass("active")){
				me.submitTopic($(this).parent().data("id"), this, "add");
			}
		});
		$(".topic .off").off().on("click", function(e){
			if(!$(this).hasClass("active")){
				me.submitTopic($(this).parent().data("id"), this, "remove");
			}
		});
	},
	submitTopic: function(id, me, action){
		var profileId = $(".frm #profileId").val(), $el = $(me).parent(),
			me = this;

			formData = '';
			formData = formData + '{"pro_id": "'+ profileId +'",';
			formData = formData + '"top_id": "'+ id +'",';
			formData = formData + '"action": "'+ action +'" }';
			//manageTopics
			$.ajax({
			    url: $(".topicsWrapper #manageURL").val(),
			    dataType: 'json',
			    type: 'POST',
			    contentType: 'application/json',
			    data: formData,
			    complete: function(data) {
			    	var responseText = JSON.parse(data.responseText);
			        if (responseText.responseCode === "000") {
			        	var alert = "";
			        	if(action == "add"){
			        		$el.before('<div class="alert alert-success">topic added.</div>');
			        	}else{
			            	$el.before('<div class="alert alert-danger">topic removed.</div>');
			        	}

			        	alert = $($el).parent().find(".alert");
			        	$(alert).fadeOut(1000, function(){
			        		$(alert).remove();
			        	});
			        	
			        } else {
			    		base.showError("There was an error.");
			        }
			    }
			});
	}
};